package MyStack;

import java.util.Arrays;

public class MyStack {
    private int[] elem;
    private int usedSize;
    private static final int DEFAULT_SIZE = 10;

    public MyStack() {
        elem = new int[DEFAULT_SIZE];
    }

    public void push(int val) {
        if (isFull()) {
            grow();
        }
        elem[usedSize] = val;
        usedSize++;
    }
    private boolean isFull() {
         return elem.length == usedSize;
    }
    private void grow() {
        elem = Arrays.copyOf(elem,elem.length);
    }

    public int pop() {
        if (isEmpty()) {
            throw new StackEmptyException("栈为空异常");
        }
        usedSize--;
        return elem[usedSize];
    }
    public boolean isEmpty() {
        return usedSize == 0;
    }

    public int peek() {
        if (!isEmpty()) {
            return elem[usedSize-1];
        }
        throw new StackEmptyException("栈为空异常");
    }
    public int size() {
        return usedSize;
    }
}
